package com.youyi.flymewallpager.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by xzp on 16/5/11.
 */
public class DividerItemDecoration extends RecyclerView.ItemDecoration{
    private Drawable mDivider;

    public DividerItemDecoration(Drawable divider) {
        mDivider=divider;
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int childCount=parent.getChildCount();
        for(int i=0;i<childCount;i++){
            View child=parent.getChildAt(i);
            int position=parent.getChildAdapterPosition(child);
            if(canDraw(position)){
                mDivider.setBounds(child.getLeft(),child.getBottom(),child.getRight(),child.getBottom()+mDivider.getIntrinsicHeight());
                mDivider.draw(c);
            }
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position=parent.getChildAdapterPosition(view);
        if(canDraw(position))
            outRect.set(0,0,0,mDivider.getIntrinsicHeight());
    }

    public boolean canDraw(int position){
        return true;
    }
}
